package com.highper.common.globalization;

import com.highper.common.util.ServletUtil;
import com.highper.common.util.WebFluxUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.MessageSource;
import org.springframework.context.NoSuchMessageException;

import java.util.Locale;

/**
 * @Author ZhangGJ
 * @Date 2022/01/25 10:10
 */
@Slf4j
public class Globalization {

    private static MessageSource messageSource;

    public Globalization(MessageSource messageSource) {
        Globalization.messageSource = messageSource;
    }

    public static String language(String message) {
        String result = null;
        try {
            String language = ServletUtil.language();
            String industry = "default";
            String globalization = industry + "_" + language;
            Locale locale = new Locale(globalization);
            result = messageSource.getMessage(message, null, locale);
        } catch (Exception e) {
            log.error("国际化失败!");
            if (!(e instanceof NoSuchMessageException)) {
                throw new RuntimeException("国际化失败!");
            }
        }
        return result;
    }

    public static String language(String message, Object[] args) {
        String result = null;
        try {
            String language = ServletUtil.language();
            String industry = "default";
            String globalization = industry + "_" + language;
            Locale locale = new Locale(globalization);
            result = messageSource.getMessage(message, args, locale);
        } catch (Exception e) {
            log.error("国际化失败!");
            if (!(e instanceof NoSuchMessageException)) {
                throw new RuntimeException("国际化失败!");
            }
        }
        return result;
    }

    public static String language(String message, String industry, String language) {
        String result = null;
        try {
            String globalization = industry + "_" + language;
            Locale locale = new Locale(globalization);
            result = messageSource.getMessage(message, null, locale);
        } catch (Exception e) {
            log.error("国际化失败!");
            if (!(e instanceof NoSuchMessageException)) {
                throw new RuntimeException(
                        "国际化失败!message：" + message + "\tindustry：" + industry + "\tlanguage："
                                + language);
            }
        }
        return result;
    }

    public static String language(String message, String industry, String language,
            Object[] args) {
        String result = null;
        try {
            String globalization = industry + "_" + language;
            Locale locale = new Locale(globalization);
            result = messageSource.getMessage(message, args, locale);
        } catch (Exception e) {
            log.error("国际化失败!");
            if (!(e instanceof NoSuchMessageException)) {
                throw new RuntimeException("国际化失败!");
            }
        }
        return result;
    }

    public static String languageWebFlux(String message) {
        String result = null;
        try {
            String language = WebFluxUtil.languageWebFlux();
            String industry = "default";
            String globalization = industry + "_" + language;
            Locale locale = new Locale(globalization);
            result = messageSource.getMessage(message, null, locale);
        } catch (Exception e) {
            log.error("国际化失败!");
            if (!(e instanceof NoSuchMessageException)) {
                throw new RuntimeException("国际化失败!");
            }
        }
        return result;
    }
}
